<template>
  <div class="background">
    <!-- 登录页面 -->
    <div class="login">
      <!-- 标题 -->
      <h3 class="title">顺通养老院管理系统</h3>
      <!-- 表单 -->
      <el-form class="formStyle" :model="state.form" ref="form" :rules="state.rules">
        <el-form-item class="formItemStyle" prop="username">
          <el-input placeholder="请输入用户名" v-model="state.form.username" clearable @keyup.enter="usernameSubmit">
            <template #prefix>
              <el-icon style="margin:8px 10px 0 0"><UserFilled /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item class="formItemStyle" prop="password">
          <el-input type="password" placeholder="请输入密码" v-model="state.form.password" clearable @keyup.enter="passwordSubmit" ref="inputPassword">
            <template #prefix>
              <el-icon style="margin:8px 10px 0 0;color:'#DEDEDE'"><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
      </el-form>

      <div>
        <!-- 登录 -->
        <el-button class="btn1" @click="handleLogin">登录</el-button>
        <!-- 注册 -->
        <el-button class="btn2" @click="handleRegister">注册</el-button>
      </div>
      
    </div>
  </div>
  
</template>
<script lang="ts">
import { defineComponent , reactive , ref } from 'vue'
import { UserFilled , Lock } from '@element-plus/icons-vue'
import axios from 'axios'
export default defineComponent({
  name:'login',
  components:{
    UserFilled,
    Lock,
  },
  setup() {
    const state = reactive({
      form:{
        username:'' as string,
        password:'' as string,
      },

      rules:{
        username:[{required:true,message: '请输入用户名',trigger: 'blur'}],
        password:[{required:true,message: '请输入密码',trigger: 'blur'}],
      }
    })
    const form = ref()
    const inputPassword = ref()
    // 设置点击回车也可登录
    const usernameSubmit = ():void => {
      if(state.form.username && state.form.password){
        handleLogin()
      }else if(state.form.username && !state.form.password){
        inputPassword.value.focus();
      }
    }
    // 设置点击回车也可登录
    const passwordSubmit = ():void => {
      if(state.form.username && state.form.password){
        handleLogin()
      }
    }
    // 处理登录事件
    const handleLogin = ():void => {
      
      form.value.validate(async (valid:boolean)=>{
        if(!valid){
          return
        }else{
          await axios({
						method:'post',
						url: 'http://127.0.0.1:13520/api/user/login',
						data: {
							username: state.form.username,
							password: state.form.password
						}
					})
					.then( res => {
						switch(res.data){
							case 0: 
                // alert('登陆成功')
                // 路由跳转
                window.location.href = '/system'
								break;
							case -1:
                state.form.username = ''
                state.form.password = ''
                alert('用户名不存在')
								break;
							case 1:
                state.form.username = ''
                state.form.password = ''
                alert('用户名或密码错误')
								break;
						}
					})
					.catch( err => {
						console.log(err);
					})
        }
      })
    }
    // 处理注册事件
    const handleRegister = ():void => {
      form.value.validate(async (valid:boolean)=>{
        if(!valid){
          return
        }else{
          await axios({
						method:'post',
						url: 'http://127.0.0.1:13520/api/user/add',
						data: {
							username: state.form.username,
							password: state.form.password
						}
					})
					.then( res => {
						switch(res.data){
							case 0: 
                alert("注册成功！！！")
                handleLogin()
								break;
							case -1:
                state.form.username = ''
                state.form.password = ''
                alert('用户名已存在')
								break;
            }
					})
					.catch( err => {
						console.log(err);
					})
        }
      })
    }
    return {
      state,
      inputPassword,
      usernameSubmit,
      passwordSubmit,
      handleLogin,
      handleRegister,
      form,
    }
  },
})
</script>
<style lang="scss" scoped>
// 清除默认样式
*{
  /*  无法选中，整体感更强 */
  user-select: none;
}
// 整个页面背景
.background{
  background-image: url("@/assets/login.jpg");
  background-size: 100% 100%;
  background-attachment: fixed;
  width:100%;
  height:100vh;
}
/* .login::after{
  content:"";
  clear: both;
  display: block;
  height: 0;
  overflow: hidden;
  visibility: hidden;
} */
// 登录框
.login{
  position: absolute;
  top: 50%;
  margin-top: -200px;
  left: 50%;
  margin-left: -200px;
  /* absolute居中的一种方法 */
  background-color: whitesmoke;
  width: 400px;
  height: 350px;
  border-radius: 25px;
  text-align: center;
  padding:  40px 20px;
  /* 这样padding就不会影响大小 */
  box-sizing: border-box;
}
// 标题
.title{
  height:30px;
  font-size:25px;
}
// 表单样式
.formStyle{
  margin-top:45px;
}
// 表单项样式
.formItemStyle{
  margin:40px 0;
}

// p{
//   height: 90px;
//   font-size: 42px;
//   font-weight: 600;
// }
// input{
//   background-color: whitesmoke;
//   width: 100%;
//   height: 48px;
//   margin-bottom: 10px;
//   border: none;
//   border-bottom: 2px solid silver;
//   outline: none;
//   font-size: 22px;
// }
// 登录注册按钮
.btn1 , .btn2{
  background-color: #59c2c5;
  width: 28%;
  height: 38px;
  border-radius: 8px;
  margin-top: 10px;
  font-size: 18px;
  font-weight: 600;
  color: white;
}
.btn1:hover , .btn2:hover{
  background-color: #59c2a0;
}
.btn1{
  margin-left: 80px;
}
.btn2{
  margin-left: 20px;
}
// :deep(.el-input__inner){
//   width: 90% !important;
// }
:deep(.el-input__icon){
  margin-top: 8px;
}
</style>